﻿
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;


namespace Tool
{
    public class FixedFunction 
    {
        #region 文件读写相关函数

        #region 功能：CSV文件写入
        public static void WriteCsvFile(string Filename, string Savedata)
        {
            StreamWriter rs = new StreamWriter(Filename, true, Encoding.GetEncoding("GB2312"));
            try
            {
                rs.WriteLine(Savedata);
                rs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件写入失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rs.Close();
            }
        }
        public  void ReWriteCsvFile(string Filename, string Savedata)
        {
            StreamWriter rs = new StreamWriter(Filename, false);
            try
            {
                rs.WriteLine(Savedata);
                rs.Close();
               
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件读取失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rs.Close();
            }
        }
        public void WriteIOCsvFile(string Filename, string Savedata)
        {
            StreamWriter rs = new StreamWriter(Filename, true, Encoding.GetEncoding("GB2312"));
            try
            {
                rs.WriteLine(Savedata);
                rs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件写入失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rs.Close();
            }
        }
        public void WriteIOCsvFile(string Filename, string Savedata, bool Bol)
        {
            StreamWriter rs = new StreamWriter(Filename, Bol, Encoding.GetEncoding("GB2312"));
            try
            {
                rs.WriteLine(Savedata);
                rs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件写入失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rs.Close();
            }
        }
        #endregion
        #region 功能：CSV文件读取
        public static string[] ReadCsvFile(string Filename)
        {
            List<string> dt = new List<string>();

            FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs,Encoding.UTF8);
            //记录每次读取的一行记录
            string strLine = "";
            string[] arr = new string[] { };
            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                string dc = strLine;
                arr = strLine.Split('_');
                dt.Add(dc);
            }
            
            string[] Arry = new string[dt.Count];
            for (int i = 0; i < dt.Count; i++)
            {
                Arry[i] = dt[i];
            }
            sr.Close();
            fs.Close();
            return arr;
        }


        public string[] ReadIOCsvFile(string Filename)
        {
            List<string> dt = new List<string>();
            FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312"), true);
            //记录每次读取的一行记录
            string strLine = "";
            //逐行读取CSV中的数据
            while ((strLine = sr.ReadLine()) != null)
            {
                string dc = strLine;
                dt.Add(dc);
            }
            string[] Arry = new string[dt.Count];
            for (int i = 0; i < dt.Count; i++)
            {
                Arry[i] = dt[i];
            }
            sr.Close();
            fs.Close();
            return Arry;
        }

        #endregion

        #region 功能：TXT文件写入,读取
        public void WriteDattxt(string Filename, string WriteData)
        {

            StreamWriter sr = new StreamWriter(Filename, true);
            try
            {
                sr.WriteLine(WriteData);
                sr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件写入失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                sr.Close();
            }

        }

        public string ReadDattxt(string Filename, string WriteData)
        {
            string readdata = "";
            string readline;
            StreamReader rs = new StreamReader(Filename, true);
            try
            {
                do
                {
                    readline = rs.ReadLine();
                    readdata += readline;

                }
                while (!rs.EndOfStream);
                rs.Close();
                return readdata;
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件写入失败：" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                rs.Close();
                return null;
            }

        }

        #endregion

        #region 功能：INI文件写入 读取
        public void IniWriteValue(string iniFile, string section, string key, string value)
        {
            InterfaceAPI.WritePrivateProfileString(section, key, value, iniFile);
        }

        public string IniGetValue(string iniFile, string section, string key, string defaultValue)
        {
            StringBuilder sb = new StringBuilder(10240);
            string value = defaultValue;
            int rtn = InterfaceAPI.GetPrivateProfileString(section, key, defaultValue, sb, 10240, iniFile);
            if (rtn != 0)
            {
                value = sb.ToString();
            }
            return value;
        }

        public decimal IniGetValue(string iniFile, string section, string key, decimal defaultValue)
        {
            StringBuilder sb = new StringBuilder(10240);
            string value = defaultValue.ToString();
            string text = Convert.ToString(defaultValue);
            int rtn = InterfaceAPI.GetPrivateProfileString(section, key, text, sb, 10240, iniFile);
            if (rtn != 0)
            {
                value = sb.ToString();
            }
            return Convert.ToDecimal(value);
        }
        public int IniGetValue(string iniFile, string section, string key, int defaultValue)
        {
            StringBuilder sb = new StringBuilder(10240);
            string value = defaultValue.ToString();
            string text = Convert.ToString(defaultValue);
            int rtn = InterfaceAPI.GetPrivateProfileString(section, key, text, sb, 10240, iniFile);
            if (rtn != 0)
            {
                value = (sb).ToString();
            }
            return Convert.ToInt32(value);
        }
        #endregion

        #region 功能：删除INI Section
        public void DelIniSec(string iniFile, string SectionName)
        {
            string text = null;
            string text2 = null;
            bool retval = InterfaceAPI.WritePrivateProfileString(SectionName, text, text2, iniFile);
        }
        #endregion


        #endregion
    }
}
